import { createApp } from 'vue'
import App from './App.vue'
import { createRouter, createWebHistory } from 'vue-router'
import { pluginRoutes } from './router'
import './assets/css/reset.css'
const app = createApp(App);

const pluginBase = '/' + __PLUGIN_NAME__;

function addPrefixToRoutes(routes:any, prefix:any) {
  const newRoutes = routes.map((route:any) => {
    const newRoute = {
      ...route,
      path: prefix + (route.path.startsWith('/') ? route.path : '/' + route.path)
    };
    if (route.redirect) {
      if (typeof route.redirect === 'string' && route.redirect.startsWith('/')) {
        newRoute.redirect = prefix + route.redirect;
      } else {
        newRoute.redirect = route.redirect;
      }
    }
    if (route.children) {
      // 子路由的 path 不加前缀，只保留原有结构
      newRoute.children = route.children;
    }
    return newRoute;
  });
  // 加入根目录
  newRoutes.unshift({
    path: '/',
    redirect: pluginBase + '/home'
  })
  return newRoutes
}

const routesWithPrefix = addPrefixToRoutes(pluginRoutes, pluginBase);

const router = createRouter({
  history: createWebHistory(),
  routes: routesWithPrefix
})


app.use(router)
app.mount('#app')